﻿package org.nickro.project.utils
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import org.nickro.externallibrary.tween.TweenLite;
	import org.nickro.externallibrary.tween.easing.*;
	/**
	 * ...
	 * @author com.nickro
	 */
	public class MovieClipUtils
	{
		/**
		 * 移动MovieClip到指定帧/帧标签
		 * @param	mc			影片剪辑
		 * @param	frame		指定帧/帧标签
		 * @param	todo		完成执行函数
		 * @param	todoParams	完成执行函数参数
		 * @param	time		移动的时间(s)。为0时表示使用帧时间(fps)进行移动
		 */
		public static function moveMcToFrame(mc:MovieClip, frame:Object, todo:Function = null, todoParams:Array = null, time:int = 0 ) {
			if(checkFrameChange(mc, frame)) return;
			if (mc == null)
				throw new Error("Error [MovieClipUtils] mc null!.");
			if (frame == null)
				throw new Error("Error [MovieClipUtils] frame null!.");
			var _time:Number;
			var frameRate:int = mc.stage.frameRate;
			var endFrame:int;
			if (!isNaN(Number(frame))){
				endFrame = int(frame);
			}else {
				endFrame = convertLabelToFrame(mc, String(frame));
			}
			if (time == 0) {
				//帧时间
				_time = Math.abs(mc.currentFrame - endFrame) / frameRate;
			}else {
				//秒时间
				_time = time;
			}
			
			TweenLite.to(mc, _time, { frame:endFrame, onComplete:todo, onCompleteParams:todoParams, ease:Linear.easeNone } );
		}
		private static var _mc:MovieClip = new MovieClip();
		private static var _isRunning:Boolean = false;
		private static var framesData:Array = [];
		/**
		 * 影片剪辑在指定帧/帧标签执行函数
		 * @param	mc		影片剪辑
		 * @param	frame	指定帧/帧标签
		 * @param	todo	执行函数
		 */
		public static function callAtFrame(mc:MovieClip, frame:Object, todo:Function,todoParams:Array = null):void {
			var endFrame:int;
			if (!isNaN(Number(frame)))
				endFrame = int(frame);
			else
				endFrame = convertLabelToFrame(mc, String(frame));
			
			var mcObj:Object = { };
			mcObj.target = mc;
			mcObj.frame = endFrame;
			mcObj.todo = todo;
			mcObj.todoParams = todoParams;
			framesData.push(mcObj);
			
			if (!_isRunning) {
				_isRunning = true;
				_mc.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		private static function onEnterFrame(e:Event):void {
			var i:int = 0;
			while (i < framesData.length) {
				if (framesData[i].target.currentFrame == framesData[i].frame) {
					try {
						framesData[i].todo.apply(null, framesData[i].todoParams);
					}catch(e:Error){
						throw new Error("[MovieClipUtils] " + e.message.toString());
					}finally{
						framesData.splice(i, 1);
					}
				}
				i++;
			}
			if (framesData.length == 0) {
				_isRunning = false;
				_mc.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		/**
		 * 返回MovieClip的指定帧标签所在帧的帧数
		 * @param	mc			影片剪辑
		 * @param	frameLabel	帧标签
		 * @return				帧数
		 */
		public static function convertLabelToFrame(mc:MovieClip, frameLabel:String):int {
			var frame:int = mc.currentFrame;
			var labels:Array = mc.currentLabels;
			for (var i:int = labels.length - 1; i > -1; i--) {
				if (labels[i].name == frameLabel) {
					frame = labels[i].frame;
					break;
				}
			}
			return frame;
		}
		
		public static function checkFrameChange(mc:MovieClip, frame:Object):Boolean {
			if (!isNaN(Number(frame))) {
				if (mc.currentFrame == int(frame)) return (true);
			}else {
				if (mc.currentLabel == String(frame)) return (true);
			}
			return (false);
		}
	}
	
}